﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using scheduleMVC.Models;
using scheduleMVC.DAL;

namespace scheduleMVC.Controllers
{
    public class FileController : Controller
    {
        private FileContext db = new FileContext();
        private ApplicationDbContext db2 = new ApplicationDbContext();

        // GET: /File/
        public ActionResult Index()
        {
            var files = db.Files.Include(f => f.Customer).Include(f => f.Task);
            return View(files.ToList());
        }

        // GET: /File/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            FileModel filemodel = db.Files.Find(id);
            if (filemodel == null)
            {
                return HttpNotFound();
            }
            return View(filemodel);
        }

        
        // GET: /File/Create 
        public ActionResult Upload(String taction, String tcontroller)
        {
            var MyFileModel = TempData["Filemodel"] as FileModel;
            MyFileModel.FileID = Guid.NewGuid().ToString();
            if (ModelState.IsValid)
            {
                
                db.Files.Add(MyFileModel);
                db.SaveChanges();

                return RedirectToAction("UploadFile", "DropBox", new { FolderName = MyFileModel.FolderPath, FilePath = MyFileModel.Path,Faction = taction,Fcontroller = tcontroller  });
            }
            return View();
        }

        

        // GET: /File/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            FileModel filemodel = db.Files.Find(id);
            if (filemodel == null)
            {
                return HttpNotFound();
            }
            ViewBag.CustomerID = new SelectList(db.CustomerModels, "CustomerId", "UserId", filemodel.CustomerID);
            ViewBag.TasksID = new SelectList(db.TasksModels, "TasksID", "Name", filemodel.TasksID);
            return View(filemodel);
        }

        // POST: /File/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="FileID,Name,UploadDate,Path,CustomerID,TasksID")] FileModel filemodel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(filemodel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CustomerID = new SelectList(db.CustomerModels, "CustomerId", "UserId", filemodel.CustomerID);
            ViewBag.TasksID = new SelectList(db.TasksModels, "TasksID", "Name", filemodel.TasksID);
            return View(filemodel);
        }

        // GET: /File/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            FileModel filemodel = db.Files.Find(id);
            if (filemodel == null)
            {
                return HttpNotFound();
            }
            return View(filemodel);
        }

        // POST: /File/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            FileModel filemodel = db.Files.Find(id);
            db.Files.Remove(filemodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
